package com.mfangsoft.zhuangjialong.integration.customer.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.customer.model.CustomerInfo;
import com.mfangsoft.zhuangjialong.integration.customer.model.CustomerSource;
import com.mfangsoft.zhuangjialong.integration.customer.service.ICustomerService;

@Controller
@RequestMapping("server")
public class CustomerController {
	
	@Autowired
	ICustomerService customerService;
	
	@RequestMapping(value="/selectCustomerPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Page<Map<String,Object>> selectCustomerPage(@RequestBody Page<Map<String,Object>> page){
		Page<Map<String,Object>> customerPage = customerService.selectCustomerPage(page);
		return customerPage;
	}
	
	@RequestMapping(value="/selectCustomerDetail",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<CustomerInfo> selectCustomerDetail(@RequestBody Map<String, Object> map){
		ResponseMessage<CustomerInfo> message = new ResponseMessage<CustomerInfo>();
		try {
			CustomerInfo customerDetail = customerService.selectCustomerDetail(map);
			message.setResult(customerDetail);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/addCustomerSeller",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addCustomerSeller(@RequestBody Map<String, Object> map){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			customerService.updateCustomerSeller(map);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/selectShopSeller",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String,Object>>> selectShopSeller(@RequestBody Map<String,Object> map){
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		try {
			List<Map<String,Object>> shopSeller = customerService.selectShopSeller(Integer.valueOf(map.get("shopId").toString()));
			message.setResult(shopSeller);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="/selectFilialeByCode",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String,Object>>> selectFilialeByCode(@RequestBody Map<String,Object> map){
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		try {
			List<Map<String,Object>> shopSeller = customerService.selectFilialeByCode(map.get("region_code")+"");
			message.setResult(shopSeller);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="/selectShopByCode",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String,Object>>> selectShopByCode(@RequestBody Map<String,Object> map){
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		try {
			List<Map<String,Object>> shopSeller = customerService.selectShopByCode(map.get("region_code")+"");
			message.setResult(shopSeller);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="/selectCustomerInstallAddress",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> selectCustomerInstallAddress(@RequestBody Map<String,Object> map){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			Map<String, Object> selectCustomerInstallAddress = customerService.selectCustomerInstallAddress(Integer.valueOf(map.get("customerId").toString()));
			message.setResult(selectCustomerInstallAddress);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/allotFiliale",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> allotFiliale(@RequestBody Map<String,Object> map){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			Integer addFiliale = customerService.addFiliale(map);
			if(addFiliale==1){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
				return message;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 添加消费者来源
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addCustomerSource",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addCustomerSource(@RequestBody CustomerSource customerSource){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			Integer num = customerService.addCustomerSource(customerSource);
			if(num==1){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
				return message;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 修改消费者来源
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/updateCustomerSource",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateCustomerSource(@RequestBody CustomerSource customerSource){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			Integer num = customerService.updateCustomerSource(customerSource);
			if(num==1){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
				return message;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 消费者来源的删除
	 */
	@RequestMapping(value="/deleteCustomerSource/{customerSourceId}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> deleteCustomerSource(@PathVariable Integer customerSourceId){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			Integer num = customerService.deleteCustomerSource(customerSourceId);
			if(num==1){
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				return message;
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
				return message;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 消费者来源列表
	 */
	@RequestMapping(value="/selectCustomerSourceList",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<CustomerSource>> selectCustomerSourceList(){
		ResponseMessage<List<CustomerSource>> message = new ResponseMessage<List<CustomerSource>>();
		try {
			List<CustomerSource> list = customerService.selectCustomerSourceList();
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(list);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	@RequestMapping(value="/updateCustomerProperty",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateCustomerProperty(@RequestBody Map<String,Object> map){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			Integer i = customerService.updateCustomerProperty(map);
			if(i>0) {
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
			}else {
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	@RequestMapping(value="/selectCustomerProperty/{customerId}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> selectCustomerProperty(@PathVariable Integer customerId){
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
		try {
			Map<String, Object> map = customerService.selectCustomerProperty(customerId);
			message.setResult(map);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
}
